package com.zhuangjie.rider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhaungjie.base.utils.PropertiesCopyUtils.PropertiesCopy;
import com.zhuangjie.rider.dto.RiderSettingsUpdateDTO;
import com.zhuangjie.rider.entity.RiderSettings;
import com.zhuangjie.rider.helper.RiderModuleTokenDataProvider;
import com.zhuangjie.rider.mapper.RiderSettingsMapper;
import com.zhuangjie.rider.service.RiderSettingsService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuangjie
 * @since 2024-01-08
 */
@Service
public class RiderSettingsServiceImpl extends ServiceImpl<RiderSettingsMapper, RiderSettings> implements RiderSettingsService {

    @Override
    public RiderSettings queryCurrentRiderSettings() {
        Long currentRiderId = RiderModuleTokenDataProvider.getCurrentRiderId();
        LambdaQueryWrapper<RiderSettings> queryRiderSettingsWrapper = Wrappers.lambdaQuery(RiderSettings.class)
                .eq(RiderSettings::getRiderId, currentRiderId);
        return this.baseMapper.selectOne(queryRiderSettingsWrapper);
    }

    @Override
    public void updateNonEmptyRiderSettingsAttributes(RiderSettingsUpdateDTO riderSettingsUpdateDTO) {
        RiderSettings riderSettings = PropertiesCopy.type(RiderSettingsUpdateDTO.class, RiderSettings.class)
                .produce(riderSettingsUpdateDTO);
        LambdaUpdateWrapper<RiderSettings> updateRiderSettingsLambdaUpdateWrapper = Wrappers.lambdaUpdate(RiderSettings.class)
                .eq(RiderSettings::getRiderId, RiderModuleTokenDataProvider.getCurrentRiderId());

        if (!this.update(riderSettings,updateRiderSettingsLambdaUpdateWrapper)) {
            throw new RuntimeException("更新骑手设置失败");
        }
    }

//    @Override
//    public void updateSystemDispatchStatus(Short newStatus) {
//        LambdaUpdateWrapper<RiderSettings> updateSystemDispatchStatusWrapper = Wrappers.lambdaUpdate(RiderSettings.class)
//                .eq(RiderSettings::getRiderId, RiderModuleTokenDataProvider.getCurrentRiderId())
//                .set(RiderSettings::getSystemDispatch, newStatus);
//
//        if (!this.update(updateSystemDispatchStatusWrapper)) {
//            throw new RuntimeException("无法更新系统派单状态");
//        }
//    }
}
